var dateTimePicker = require('./data.js');
import siteinfo from '../../../siteinfo.js';
const  apiroot= siteinfo.siteroot + 'index.php?s=/api/';
Page({
  data: {
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2000,
    endYear: 2050,
    shopid:'',
    dianhua1:''
  },
  onLoad(e) {

    this.setData({
      shopid:e.shopid
    })
    console.log(e);

    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });
  },
  dianhua(e) {

    this.setData({
      dianhua1: e.detail.value
    })
  },
  manba(e){
    console.log(e.currentTarget.dataset.text) //我是内容
  },
  tijiao(e){
    var that=this

    if (that.data.dianhua1==''){
      wx.showToast({
        title: '电话不能为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }

    if (that.data.dateTime1 == '') {
      wx.showToast({
        title: '时间必填',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: apiroot +'manba/appointment', //仅为示例，并非真实的接口地址
      data: {
        shopid: that.data.shopid,
        time: e.currentTarget.dataset.text,
        phone: that.data.dianhua1,
        user_id: wx.getStorageSync('user_id')
      },
     
      success(res) {
        console.log(res.data)
        if (res.data.code==200){
          wx.showToast({
            title: '预约成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }, 2000);
        }
      }
    })
   
  },
  changeDate(e) {
    console.log(e.detail.value)
    this.setData({ date: e.detail.value });
  },
  changeTime(e) {
    console.log(e.detail.value)
    this.setData({ time: e.detail.value });
  },
  changeDateTime(e) {
    console.log(e.detail.value)
    this.setData({ dateTime: e.detail.value });
  },
  changeDateTime1(e) {
    console.log(e.detail.value)
    this.manba()
    this.setData({ dateTime1: e.detail.value });
  },
  changeDateTimeColumn(e) {
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    console.log(dateArr)
    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  }
})